package com.ruoyi.web.controller.gcgl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DecimalCalculateUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcCustomer;
import com.ruoyi.gcgl.domain.SjProcessConfig;
import com.ruoyi.gcgl.domain.SjProjectPeople;
import com.ruoyi.gcgl.domain.YyContract;
import com.ruoyi.gcgl.domain.YyProject;
import com.ruoyi.gcgl.domain.YyProjectSqkp;
import com.ruoyi.gcgl.mapper.GcSendTipMapper;
import com.ruoyi.gcgl.service.IGcCustomerService;
import com.ruoyi.gcgl.service.IGcSendTipService;
import com.ruoyi.gcgl.service.ISjProcessConfigService;
import com.ruoyi.gcgl.service.ISjProjectPeopleService;
import com.ruoyi.gcgl.service.IYyContractService;
import com.ruoyi.gcgl.service.IYyProjectService;
import com.ruoyi.gcgl.service.IYyProjectSqkpService;
import com.ruoyi.gcgl.service.IYyProjectSqkpfpService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 申请开票Controller
 * 
 * @author ruoyi
 * @date 2022-12-11
 */
@Controller
@RequestMapping("/yy/sqkp")
public class YyProjectSqkpController extends BaseController
{
    private String prefix = "yy/sqkp";

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IYyProjectSqkpService gcProjectSqkpService;
    @Autowired
    private IYyProjectService gcProjectService;
    @Autowired
    private IGcCustomerService gcCustomerService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private IGcSendTipService gcSendTipService;
    @Autowired
    private GcSendTipMapper gcSendTipMapper;
    @Autowired
    private IYyProjectSqkpfpService gcProjectSqkpfpService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Autowired
    private ISjProcessConfigService sjProcessConfigService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private IYyContractService yyContractService;
    
    final String processCode = "sjkaipiaoshenqing";
    @Autowired
    private ISjProjectPeopleService sjProjectPeopleService;
    
    @GetMapping()
    public String sqkp()
    {
        return prefix + "/sqkp";
    }

    /**
     * 查询申请开票列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YyProjectSqkp gcProjectSqkp)
    {
        startPage();
        gcProjectSqkp.setDeleteFlag(1);
      //角色数据权限 ==根据用户定义的数据权限查看项目数据
        List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
        int count = roleService.selectCountRoleDeptByRoleId(roleIds);
        if(count != 0){
        	//看部门
        	gcProjectSqkp.setRoleIds(roleIds);
        }else{
        	//看个人
        	if(getSysUser().isShowRoleIds(roleIds)){
        		gcProjectSqkp.setOwnerId(getUserId());
            }
        }
        //----
        List<YyProjectSqkp> list = gcProjectSqkpService.selectGcProjectSqkpList(gcProjectSqkp);
        return getDataTable(list);
    }

    /**
     * 导出申请开票列表
     */
    @Log(title = "申请开票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YyProjectSqkp gcProjectSqkp)
    {
    	gcProjectSqkp.setDeleteFlag(1);
    	if(getSysUser().isShow()){
            gcProjectSqkp.setCreateUser(getLoginName());
            }
        List<YyProjectSqkp> list = gcProjectSqkpService.selectGcProjectSqkpList(gcProjectSqkp);
        ExcelUtil<YyProjectSqkp> util = new ExcelUtil<YyProjectSqkp>(YyProjectSqkp.class);
        return util.exportExcel(list, "申请开票数据");
    }

    /**
     * 新增申请开票
     */
    @GetMapping("/add/{id}")
    public String add(ModelMap mmap,@PathVariable("id") Long id){
    	YyContract yyContract = yyContractService.selectSjContractById(id);
    	mmap.put("sjContract", yyContract);
    	
    	mmap.put("gcProjectXmxq", null);
    	
    	mmap.put("sqsj", new Date());
    	
    	GcCustomer gcCustomer = new GcCustomer();
    	gcCustomer.setDeleteFlag(1);
    	gcCustomer.setName(yyContract.getCustomerName());
    	List<GcCustomer> lis = gcCustomerService.selectGcCustomerList(gcCustomer);
    	if(lis!=null && lis.size()>0){
    		mmap.put("shuih", lis.get(0).getCkdw()+"/"+lis.get(0).getNssbh());//单位信息名称/税号
        	mmap.put("kaihuh", lis.get(0).getKhyh()+"/"+lis.get(0).getSkzh());//单位信息开户行/帐号
        	mmap.put("dizhi", lis.get(0).getAddress()+"/"+lis.get(0).getPhone());//单位信息地址/电话
    	}else{
    		mmap.put("shuih", "");//单位信息名称/税号
        	mmap.put("kaihuh", "");//单位信息开户行/帐号
        	mmap.put("dizhi", "");//单位信息地址/电话
    	}
    	
    	double ykfpje=0.0;//已开发票金额
    	String ykfpsj = "";//已开发票时间
    	YyProjectSqkp sqkp = new YyProjectSqkp();
        sqkp.setGcProjectid(yyContract.getGcProjectId());
        sqkp.setDeleteFlag(1);
        String[] statuslis = new String[]{"1","2","3","9"};
        sqkp.setStatuslis(statuslis);
        List<YyProjectSqkp> sqkplis = gcProjectSqkpService.selectGcProjectSqkpList(sqkp);
        for(YyProjectSqkp sqkp1:sqkplis){
        	sqkp1.setCreateUser(DateUtils.dateTime(sqkp1.getSqsj()));
        	ykfpje=DecimalCalculateUtil.add(ykfpje, Double.valueOf(sqkp1.getItemje().toString()));
        	ykfpsj=DateUtils.dateTime(sqkp1.getSqsj());
        }
        mmap.put("sqkplis", sqkplis);
        
        BigDecimal bigDecimal = new BigDecimal(ykfpje);
        String s = bigDecimal.toPlainString();
        mmap.put("ykfpje", s);
        mmap.put("ykfpsj", ykfpsj);
        
        //下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        
      //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        
        return prefix + "/add";
    }
    
    /**
     * 新增保存申请开票
     */
    @Log(title = "申请开票", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YyProjectSqkp gcProjectSqkp){
    	
    	try {
    		
        	gcProjectSqkp.setOwner(getLoginName());
        	gcProjectSqkp.setOwnerId(getUserId());
        	gcProjectSqkp.setDept(getSysUser().getDept().getDeptName());
        	gcProjectSqkp.setDeptId(getSysUser().getDept().getDeptId());
    		gcProjectSqkp.setXmfzr(getLoginName());
        	gcProjectSqkp.setXmfzrAt(new Date());
        	gcProjectSqkp.setStatus(1);//状态1
        	gcProjectSqkp.setDeleteFlag(1);
        	gcProjectSqkp.setCreateAt(new Date());
        	gcProjectSqkp.setCreateUser(getLoginName());
        	gcProjectSqkp.setUpdateAt(new Date());
        	gcProjectSqkp.setUpdateUser(getLoginName());
        	
        	gcProjectSqkpService.insertGcProjectSqkp(gcProjectSqkp);
        	
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return toAjax(1);
		}
    	
        return toAjax(1);
    }

    /**
     * 修改申请开票
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        YyProjectSqkp gcProjectSqkp = gcProjectSqkpService.selectGcProjectSqkpById(id);
        mmap.put("htje", DecimalCalculateUtil.returnEje(gcProjectSqkp.getHtje()));
        mmap.put("gcProjectSqkp", gcProjectSqkp);
        
        YyProjectSqkp sqkp = new YyProjectSqkp();
        sqkp.setGcProjectid(gcProjectSqkp.getGcProjectid());
        sqkp.setDeleteFlag(1);
        String[] statuslis = new String[]{"1","2","3","9"};
        sqkp.setStatuslis(statuslis);
        List<YyProjectSqkp> sqkplis = gcProjectSqkpService.selectGcProjectSqkpList(sqkp);
        for(YyProjectSqkp sqkp1:sqkplis){
        	sqkp1.setCreateUser(DateUtils.dateTime(sqkp1.getSqsj()));
        }
        
        mmap.put("sqkplis", sqkplis);
        
        //下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        
      //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        return prefix + "/edit";
    }
    
    /**
     * 修改申请开票
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        YyProjectSqkp gcProjectSqkp = gcProjectSqkpService.selectGcProjectSqkpById(id);
        
        YyProjectSqkp sqkp = new YyProjectSqkp();
        sqkp.setGcProjectid(gcProjectSqkp.getGcProjectid());
        sqkp.setDeleteFlag(1);
        List<YyProjectSqkp> sqkplis = gcProjectSqkpService.selectGcProjectSqkpList(sqkp);
        
        double heje = 0.0;
        if(sqkplis!=null && sqkplis.size()<7){
        	for(YyProjectSqkp obj:sqkplis){
        		obj.setCreateUser(DateUtils.dateTime(obj.getSqsj()));
        		heje = DecimalCalculateUtil.add(heje, Double.valueOf(obj.getItemje().toString()));
        	}
        }
        mmap.put("sqkplis", sqkplis);
        gcProjectSqkp.setHej(heje);
        mmap.put("htje", DecimalCalculateUtil.returnEje(gcProjectSqkp.getHtje()));
        mmap.put("gcProjectSqkp", gcProjectSqkp);
        
      //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        return prefix + "/detail";
    }

    /**
     * 修改保存申请开票
     */
    @Log(title = "申请开票", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YyProjectSqkp gcProjectSqkp){
    	gcProjectSqkp.setZjlsp("");
    	gcProjectSqkp.setCwkpr("");
    	gcProjectSqkp.setUpdateAt(new Date());
    	gcProjectSqkp.setUpdateUser(getLoginName());
        return toAjax(gcProjectSqkpService.updateGcProjectSqkp(gcProjectSqkp));
    }

    /**
     * 删除申请开票
     */
    @Log(title = "申请开票", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcProjectSqkpService.deleteGcProjectSqkpByIds(ids));
    }
    
    //----------工程
    /**
     * 申请开票评审
     * @return
     */
    @GetMapping("/gcps")
    public String sqkpgcps(){
        return prefix + "/sqkpgcps";
    }
    
    /**
     * 查询申请开票评审列表
     */
    @PostMapping("/gcpslist")
    @ResponseBody
    public TableDataInfo sqkpgcpslist(YyProjectSqkp gcProjectSqkp)
    {
        startPage();
        gcProjectSqkp.setDeleteFlag(1);
        gcProjectSqkp.setStatus(0);
        List<YyProjectSqkp> list = gcProjectSqkpService.selectGcProjectSqkpList(gcProjectSqkp);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/gcyes")
    @ResponseBody
    public AjaxResult gcyes(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		YyProjectSqkp gcProjectSqkpObj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(id[i].toString()));
    		if(gcProjectSqkpObj!=null){
    			YyProjectSqkp gcProjectSqkp = new YyProjectSqkp();
    			gcProjectSqkp.setId(Long.parseLong(id[i]));
    			gcProjectSqkp.setStatus(1);
    			gcProjectSqkp.setSqrpr(getLoginName());
    			gcProjectSqkp.setSqrprAt(new Date());
    			gcProjectSqkp.setSpyj(spyj);
    			gcProjectSqkpService.updateGcProjectSqkp(gcProjectSqkp);
    			
    		}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/gcno")
    @ResponseBody
    public AjaxResult gcno(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		YyProjectSqkp gcProjectSqkpObj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(id[i].toString()));
    		if(gcProjectSqkpObj!=null){
    			YyProjectSqkp gcProjectSqkp = new YyProjectSqkp();
    			gcProjectSqkp.setId(Long.parseLong(id[i]));
    			gcProjectSqkp.setStatus(2);
    			gcProjectSqkp.setSqrpr(getLoginName());
    			gcProjectSqkp.setSqrprAt(new Date());
    			gcProjectSqkp.setSpyj(spyj);
    			gcProjectSqkpService.updateGcProjectSqkp(gcProjectSqkp);
            	
    		}
    	}
    	return toAjax(1);
    }
    
    //总经理
    /**
     * 申请开票评审
     * @return
     */
    @GetMapping("/ps")
    public String sqkpps(){
        return prefix + "/sqkpps";
    }
    
    /**
     * 查询申请开票评审列表
     */
    @PostMapping("/pslist")
    @ResponseBody
    public TableDataInfo sqkppslist(YyProjectSqkp gcProjectSqkp)
    {
        startPage();
        gcProjectSqkp.setDeleteFlag(1);
        gcProjectSqkp.setStatus(3);
        List<YyProjectSqkp> list = gcProjectSqkpService.selectGcProjectSqkpList(gcProjectSqkp);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/yes")
    @ResponseBody
    public AjaxResult yes(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		YyProjectSqkp gcProjectSqkpObj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(id[i].toString()));
    		if(gcProjectSqkpObj!=null){
    			YyProjectSqkp gcProjectSqkp = new YyProjectSqkp();
    			gcProjectSqkp.setId(Long.parseLong(id[i]));
    			gcProjectSqkp.setStatus(5);
    			gcProjectSqkp.setZjlsp(getLoginName());
    			gcProjectSqkp.setZjlspAt(new Date());
    			gcProjectSqkp.setSpyj(spyj);
    			gcProjectSqkpService.updateGcProjectSqkp(gcProjectSqkp);
    			
    		}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/no")
    @ResponseBody
    public AjaxResult no(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		YyProjectSqkp gcProjectSqkpObj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(id[i].toString()));
    		if(gcProjectSqkpObj!=null){
    			YyProjectSqkp gcProjectSqkp = new YyProjectSqkp();
    			gcProjectSqkp.setId(Long.parseLong(id[i]));
    			gcProjectSqkp.setStatus(6);
    			gcProjectSqkp.setZjlsp(getLoginName());
    			gcProjectSqkp.setZjlspAt(new Date());
    			gcProjectSqkp.setSpyj(spyj);
    			gcProjectSqkpService.updateGcProjectSqkp(gcProjectSqkp);
            	
    		}
    	}
    	return toAjax(1);
    }
    
    //////会计
    /**
     * 申请开票评审
     * @return
     */
    @GetMapping("/hjps")
    public String sqkphjps(){
        return prefix + "/sqkphjps";
    }
    
    /**
     * 查询申请开票评审列表
     */
    @PostMapping("/hjpslist")
    @ResponseBody
    public TableDataInfo sqkphjpslist(YyProjectSqkp gcProjectSqkp)
    {
        startPage();
        gcProjectSqkp.setDeleteFlag(1);
        String[] statuslis = new String[]{"1","3","5","7"};
        gcProjectSqkp.setStatuslis(statuslis);
        List<YyProjectSqkp> list = gcProjectSqkpService.selectGcProjectSqkpList(gcProjectSqkp);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/hjyes")
    @ResponseBody
    public AjaxResult hjyes(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		YyProjectSqkp gcProjectSqkpObj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(id[i].toString()));
    		if(gcProjectSqkpObj!=null){
    			YyProjectSqkp gcProjectSqkp = new YyProjectSqkp();
    			gcProjectSqkp.setId(Long.parseLong(id[i]));
    			gcProjectSqkp.setStatus(3);
    			gcProjectSqkp.setCwkpr(getLoginName());
    			gcProjectSqkp.setCwkprAt(new Date());
    			gcProjectSqkp.setSpyj(spyj);
    			gcProjectSqkpService.updateGcProjectSqkp(gcProjectSqkp);
    			
    		}
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/hjno")
    @ResponseBody
    public AjaxResult hjno(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		YyProjectSqkp gcProjectSqkpObj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(id[i].toString()));
    		if(gcProjectSqkpObj!=null){
    			YyProjectSqkp gcProjectSqkp = new YyProjectSqkp();
    			gcProjectSqkp.setId(Long.parseLong(id[i]));
    			gcProjectSqkp.setStatus(4);
    			gcProjectSqkp.setCwkpr(getLoginName());
    			gcProjectSqkp.setCwkprAt(new Date());
    			gcProjectSqkp.setSpyj(spyj);
    			gcProjectSqkpService.updateGcProjectSqkp(gcProjectSqkp);
            	
    		}
    	}
    	return toAjax(1);
    }
    
    /**
     * 增值税发票开具申请单
     */
    @GetMapping("/sqkpprint/{id}")
    public String sqkpprint(@PathVariable("id") Long id, ModelMap mmap){
        YyProjectSqkp gcProjectSqkp = gcProjectSqkpService.selectGcProjectSqkpById(id);
        
        String projectType = dictDataService.selectDictLabel("sj_nsct_type", gcProjectSqkp.getXmlx());
        String fpType = dictDataService.selectDictLabel("gcgl_project_fp", gcProjectSqkp.getFplx());
        mmap.put("projectType", projectType);
        mmap.put("fpType", fpType);
        
        YyProjectSqkp sqkp = new YyProjectSqkp();
        sqkp.setGcProjectid(gcProjectSqkp.getGcProjectid());
        sqkp.setDeleteFlag(1);
        String[] statuslis = new String[]{"1","2","3","9"};
        sqkp.setStatuslis(statuslis);
        List<YyProjectSqkp> sqkplis = gcProjectSqkpService.selectGcProjectSqkpList(sqkp);
        
        double heje = 0.0;
        if(sqkplis!=null && sqkplis.size()<7){
        	
        	for(YyProjectSqkp obj:sqkplis){
        		heje = DecimalCalculateUtil.add(heje, Double.valueOf(obj.getItemje().toString()));
        	}
        	
        	for(int i=sqkplis.size();i<=7;i++){
        		YyProjectSqkp obj = new YyProjectSqkp();
        		sqkplis.add(obj);
        	}
        }
        mmap.put("sqkplis", sqkplis);
        gcProjectSqkp.setHej(Double.parseDouble(DecimalCalculateUtil.returnEje(heje)));
        mmap.put("htje", DecimalCalculateUtil.returnEje(gcProjectSqkp.getHtje()));
        mmap.put("gcProjectSqkp", gcProjectSqkp);
        
//        GcProjectSqkpfp gcProjectSqkpfp = new GcProjectSqkpfp();
//        gcProjectSqkpfp.setDeleteFlag(1);
//        gcProjectSqkpfp.setGcProjectid(gcProjectSqkp.getGcProjectid());
//        gcProjectSqkpfp.setProjectId(id);
//        List<GcProjectSqkpfp> fplis = gcProjectSqkpfpService.selectGcProjectSqkpfpList(gcProjectSqkpfp);
//        mmap.put("fplis", fplis);
        
        return prefix + "/sqkpprint";
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/hjconfirm")
    @ResponseBody
    public AjaxResult hjconfirm(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		YyProjectSqkp gcProjectSqkpObj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(id[i].toString()));
    		if(gcProjectSqkpObj!=null){
    			YyProjectSqkp gcProjectSqkp = new YyProjectSqkp();
    			gcProjectSqkp.setId(Long.parseLong(id[i]));
    			gcProjectSqkp.setStatus(7);
    			gcProjectSqkp.setUpdateUser(getLoginName());
    			gcProjectSqkp.setUpdateAt(new Date());
    			gcProjectSqkpService.updateGcProjectSqkp(gcProjectSqkp);
    		}
    	}
    	
    	return toAjax(1);
    }
    
    @GetMapping("/sqkpzjlsp/{id}")
    public String sqkpzjlsp(@PathVariable("id") Long id, ModelMap mmap){
        YyProjectSqkp gcProjectSqkp = gcProjectSqkpService.selectGcProjectSqkpById(id);
        
        String projectType = dictDataService.selectDictLabel("sj_nsct_type", gcProjectSqkp.getXmlx());
        String fpType = dictDataService.selectDictLabel("gcgl_project_fp", gcProjectSqkp.getFplx());
        mmap.put("projectType", projectType);
        mmap.put("fpType", fpType);
        
        YyProjectSqkp sqkp = new YyProjectSqkp();
        sqkp.setGcProjectid(gcProjectSqkp.getGcProjectid());
        sqkp.setDeleteFlag(1);
        List<YyProjectSqkp> sqkplis = gcProjectSqkpService.selectGcProjectSqkpList(sqkp);
        
        double heje = 0.0;
        if(sqkplis!=null && sqkplis.size()<7){
        	
        	for(YyProjectSqkp obj:sqkplis){
        		heje = DecimalCalculateUtil.add(heje, Double.valueOf(obj.getItemje().toString()));
        	}
        	
        	for(int i=sqkplis.size();i<=7;i++){
        		YyProjectSqkp obj = new YyProjectSqkp();
        		sqkplis.add(obj);
        	}
        }
        mmap.put("sqkplis", sqkplis);
        gcProjectSqkp.setHej(heje);
        mmap.put("gcProjectSqkp", gcProjectSqkp);
        return prefix + "/sqkpzjlsp";
    }
    
    @GetMapping("/sqkphjsp/{id}")
    public String sqkphjsp(@PathVariable("id") Long id, ModelMap mmap){
        YyProjectSqkp gcProjectSqkp = gcProjectSqkpService.selectGcProjectSqkpById(id);
        
        String projectType = dictDataService.selectDictLabel("sj_nsct_type", gcProjectSqkp.getXmlx());
        String fpType = dictDataService.selectDictLabel("gcgl_project_fp", gcProjectSqkp.getFplx());
        mmap.put("projectType", projectType);
        mmap.put("fpType", fpType);
        
        YyProjectSqkp sqkp = new YyProjectSqkp();
        sqkp.setGcProjectid(gcProjectSqkp.getGcProjectid());
        sqkp.setDeleteFlag(1);
        List<YyProjectSqkp> sqkplis = gcProjectSqkpService.selectGcProjectSqkpList(sqkp);
        
        double heje = 0.0;
        if(sqkplis!=null && sqkplis.size()<7){
        	
        	for(YyProjectSqkp obj:sqkplis){
        		heje = DecimalCalculateUtil.add(heje, Double.valueOf(obj.getItemje().toString()));
        	}
        	
        	for(int i=sqkplis.size();i<=7;i++){
        		YyProjectSqkp obj = new YyProjectSqkp();
        		sqkplis.add(obj);
        	}
        }
        mmap.put("sqkplis", sqkplis);
        gcProjectSqkp.setHej(heje);
        mmap.put("gcProjectSqkp", gcProjectSqkp);
        return prefix + "/sqkphjsp";
    }
    
    @GetMapping("/sqkpgcsp/{id}")
    public String sqkpgcsp(@PathVariable("id") Long id, ModelMap mmap){
        YyProjectSqkp gcProjectSqkp = gcProjectSqkpService.selectGcProjectSqkpById(id);
        
        String projectType = dictDataService.selectDictLabel("sj_nsct_type", gcProjectSqkp.getXmlx());
        String fpType = dictDataService.selectDictLabel("gcgl_project_fp", gcProjectSqkp.getFplx());
        mmap.put("projectType", projectType);
        mmap.put("fpType", fpType);
        
        YyProjectSqkp sqkp = new YyProjectSqkp();
        sqkp.setGcProjectid(gcProjectSqkp.getGcProjectid());
        sqkp.setDeleteFlag(1);
        List<YyProjectSqkp> sqkplis = gcProjectSqkpService.selectGcProjectSqkpList(sqkp);
        
        double heje = 0.0;
        if(sqkplis!=null && sqkplis.size()<7){
        	
        	for(YyProjectSqkp obj:sqkplis){
        		heje = DecimalCalculateUtil.add(heje, Double.valueOf(obj.getItemje().toString()));
        	}
        	
        	for(int i=sqkplis.size();i<=7;i++){
        		YyProjectSqkp obj = new YyProjectSqkp();
        		sqkplis.add(obj);
        	}
        }
        mmap.put("sqkplis", sqkplis);
        gcProjectSqkp.setHej(heje);
        mmap.put("gcProjectSqkp", gcProjectSqkp);
        return prefix + "/sqkpgcsp";
    }
    
    @GetMapping("/p4sqkp/{id}")
    public String p4sqkp(ModelMap mmap,@PathVariable("id") Long id){
    	YyProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	//判断是成员，还是管理员
    	List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
    	//看个人
        int flag = 0;//成员
    	if(getSysUser().isShowRoleIds(roleIds)){
    		SjProjectPeople sjProjectPeople = new SjProjectPeople();
            sjProjectPeople.setDeleteFlag(1);
            sjProjectPeople.setGcProjectid(id);
            sjProjectPeople.setCode(getUserId().toString());
            List<SjProjectPeople> peoplelis = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
            if(peoplelis!=null && peoplelis.size()>0){
            	for(SjProjectPeople po1:peoplelis){
                	if(po1.getpId()==0l){
                		flag = 1;//领导
                		break;
                	}
                }
            }
        }else{
        	flag = 1;//领导
        }
        
        mmap.put("flag", flag);
        mmap.put("flag", flag);
        return "yy/project/projectDetailSqkp";
    }

    /**
     * 查询申请开票列表
     */
    @PostMapping("/p4list")
    @ResponseBody
    public TableDataInfo p4list(YyProjectSqkp gcProjectSqkp){
        startPage();
        gcProjectSqkp.setDeleteFlag(1);
      //角色数据权限 ==根据用户定义的数据权限查看项目数据
        List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
        int count = roleService.selectCountRoleDeptByRoleId(roleIds);
        if(count != 0){
        	//看部门
        	gcProjectSqkp.setRoleIds(roleIds);
        }else{
        	//看个人
        	if(getSysUser().isShowRoleIds(roleIds)){
        		gcProjectSqkp.setOwner(getLoginName());
            }
        }
        //----
        List<YyProjectSqkp> list = gcProjectSqkpService.selectGcProjectSqkpList(gcProjectSqkp);
        return getDataTable(list);
    }
    
    //申请人部门PID
    private List<SjProcessConfig> returnUser(Long deptId){
    	SysDept dept = deptService.selectDeptById(deptId);
    	SjProcessConfig sjProcessConfig = new SjProcessConfig();
        sjProcessConfig.setDeleteFlag(1);
        sjProcessConfig.setCode(processCode);
        sjProcessConfig.setDeptId(dept.getParentId());
        List<SjProcessConfig> configlis = sjProcessConfigService.selectSjProcessConfigList(sjProcessConfig);
		
        if(configlis==null || configlis.size()==0){
        	SjProcessConfig sjProcessConfig1 = new SjProcessConfig();
        	sjProcessConfig1.setDeleteFlag(1);
        	sjProcessConfig1.setCode("morenliucheng");
            List<SjProcessConfig> configlis1 = sjProcessConfigService.selectSjProcessConfigList(sjProcessConfig1);
            return configlis1;
        }
        return configlis;
    }
    
    /**
     * 修改保存申请开票
     */
    @Log(title = "申请开票", businessType = BusinessType.UPDATE)
    @PostMapping("/edit4check")
    @ResponseBody
    public AjaxResult edit4check(YyProjectSqkp gcProjectSqkp){
    	gcProjectSqkp.setUpdateAt(new Date());
    	gcProjectSqkp.setUpdateUser(getLoginName());
        return toAjax(gcProjectSqkpService.updateGcProjectSqkp4check(gcProjectSqkp));
    }
    
    //---------------------审批方法
    /**
     * level1
     * @return
     */
    @GetMapping("/level1")
    public String level1(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	YyProjectSqkp obj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(p.getBusinessKey().toString()));
        	mmap.put("htje", DecimalCalculateUtil.returnEje(obj.getHtje()));
        	mmap.put("obj", obj);
        	
        	GcCustomer gcCustomer = new GcCustomer();
        	gcCustomer.setDeleteFlag(1);
        	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
        	mmap.put("clis", gcCustomerlis);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            mmap.put("approveid", obj.getCwId());
            mmap.put("approvename", obj.getCw());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level1check";
    }
    
    /**
     * level2
     * @return
     */
    @GetMapping("/level2")
    public String level2(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	YyProjectSqkp obj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(p.getBusinessKey().toString()));
        	mmap.put("htje", DecimalCalculateUtil.returnEje(obj.getHtje()));
        	mmap.put("obj", obj);
        	
        	GcCustomer gcCustomer = new GcCustomer();
        	gcCustomer.setDeleteFlag(1);
        	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
        	mmap.put("clis", gcCustomerlis);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            mmap.put("approveid", obj.getFzjl1Id());
            mmap.put("approvename", obj.getFzjl1());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level2check";
    }
    /**
     * level2
     * @return
     */
    @GetMapping("/level3")
    public String level3(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	YyProjectSqkp obj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(p.getBusinessKey().toString()));
        	mmap.put("htje", DecimalCalculateUtil.returnEje(obj.getHtje()));
        	mmap.put("obj", obj);
        	
        	GcCustomer gcCustomer = new GcCustomer();
        	gcCustomer.setDeleteFlag(1);
        	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
        	mmap.put("clis", gcCustomerlis);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            mmap.put("approveid", obj.getFzjl2Id());
            mmap.put("approvename", obj.getFzjl2());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level3check";
    }
    
    /**
     * level2
     * @return
     */
    @GetMapping("/level4")
    public String level4(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	YyProjectSqkp obj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(p.getBusinessKey().toString()));
        	mmap.put("htje", DecimalCalculateUtil.returnEje(obj.getHtje()));
        	mmap.put("obj", obj);
        	
        	GcCustomer gcCustomer = new GcCustomer();
        	gcCustomer.setDeleteFlag(1);
        	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
        	mmap.put("clis", gcCustomerlis);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            mmap.put("approveid", obj.getZjlId());
            mmap.put("approvename", obj.getZjl());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level4check";
    }
    
    /**
     * level2
     * @return
     */
    @GetMapping("/level5")
    public String level5(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	YyProjectSqkp obj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(p.getBusinessKey().toString()));
        	mmap.put("htje", DecimalCalculateUtil.returnEje(obj.getHtje()));
        	mmap.put("obj", obj);
        	
        	GcCustomer gcCustomer = new GcCustomer();
        	gcCustomer.setDeleteFlag(1);
        	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
        	mmap.put("clis", gcCustomerlis);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            mmap.put("approveid", obj.getCnId());
            mmap.put("approvename", obj.getCn());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level5check";
    }
    
    /**
     * level2
     * @return
     */
    @GetMapping("/level6")
    public String level6(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	YyProjectSqkp obj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(p.getBusinessKey().toString()));
        	mmap.put("htje", DecimalCalculateUtil.returnEje(obj.getHtje()));
        	mmap.put("obj", obj);
        	
        	GcCustomer gcCustomer = new GcCustomer();
        	gcCustomer.setDeleteFlag(1);
        	List<GcCustomer> gcCustomerlis = gcCustomerService.selectGcCustomerList(gcCustomer);
        	mmap.put("clis", gcCustomerlis);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            List<SjProcessConfig> configlis = returnUser(Long.valueOf(obj.getDeptId().toString()));
            mmap.put("approveid", configlis.get(0).getLevel7id());
            mmap.put("approvename", configlis.get(0).getLevel7name());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level6check";
    }
    
    /**
     * 修改单据
     * @param taskid
     * @param mmap
     * @return
     */
    @GetMapping("/modify")
    public String modify(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	YyProjectSqkp obj = gcProjectSqkpService.selectGcProjectSqkpById(Long.parseLong(p.getBusinessKey().toString()));
        	mmap.put("htje", DecimalCalculateUtil.returnEje(obj.getHtje()));
        	mmap.put("obj", obj);
        	
        	YyProjectSqkp sqkp = new YyProjectSqkp();
            sqkp.setGcProjectid(obj.getGcProjectid());
            sqkp.setDeleteFlag(1);
            List<YyProjectSqkp> sqkplis = gcProjectSqkpService.selectGcProjectSqkpList(sqkp);
            for(YyProjectSqkp sqkp1:sqkplis){
            	sqkp1.setCreateUser(DateUtils.dateTime(sqkp1.getSqsj()));
            }
            mmap.put("sqkplis", sqkplis);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/modifycheck";
    }
}